<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="产品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入产品名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生产地" prop="productionPlace">
        <el-input
          v-model="queryParams.productionPlace"
          placeholder="请输入生产地"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="检验机构" prop="testBy">
        <el-input
          v-model="queryParams.testBy"
          placeholder="请输入检验机构"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="合格证编号" prop="code" label-width="100">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入合格证编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['nongjt:certificate:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['nongjt:certificate:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['nongjt:certificate:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['nongjt:certificate:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="certificateList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="主键" align="center" prop="id" />-->
<!--      <el-table-column label="用户注册表主键" align="center" prop="userId" />-->
      <el-table-column label="产品名称" align="center" prop="productName" />
      <el-table-column label="产品重量" align="center" prop="productWeight" />
      <el-table-column label="计量单位" align="center" prop="unit" />
      <el-table-column label="产地" align="center" prop="produceAddress" />
      <el-table-column label="生产地" align="center" prop="productionPlace" />
      <el-table-column label="检测方式" align="center" prop="testType" />
      <el-table-column label="检验机构" align="center" prop="testBy" />
      <el-table-column label="检测报告" align="center" prop="testReport" />
      <el-table-column label="用户签名" align="center" prop="userSignature" />
      <el-table-column label="合格证编号" align="center" prop="code" />
      <el-table-column label="二维码地址" align="center" prop="qrcode" />
      <el-table-column label="打印次数" align="center" prop="printNumber" />
<!--      <el-table-column label="合格证类型" align="center" prop="qrType" />-->
      <el-table-column label="是否同步到成都市 " align="center" prop="isSync">
        <template #default="scope">
          <span v-if="scope.row.isSync === '1' || scope.row.isSync === 1 || scope.row.isSync === '2' || scope.row.isSync === 2">是</span>
          <span v-else-if="scope.row.isSync === '0' || scope.row.isSync === 0">否</span>
          <span v-else>{{ scope.row.isSync }}</span>
        </template>
      </el-table-column>
      <el-table-column label="省平台追溯码" align="center" prop="traceCode" />
<!--      <el-table-column label="省平台批次类型(0 农事活动，1 生产加工，默认0)" align="center" prop="batchType" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['nongjt:certificate:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['nongjt:certificate:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改合格证管理对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body >
      <el-form ref="certificateRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="用户注册表主键" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户注册表主键" />
        </el-form-item>
        <el-form-item label="产品名称" prop="productName">
          <el-input v-model="form.productName" placeholder="请输入产品名称" />
        </el-form-item>
        <el-form-item label="产品重量" prop="productWeight">
          <el-input v-model="form.productWeight" placeholder="请输入产品重量" />
        </el-form-item>
        <el-form-item label="计量单位" prop="unit">
          <el-input v-model="form.unit" placeholder="请输入计量单位" />
        </el-form-item>
        <el-form-item label="产地" prop="produceAddress">
          <el-input v-model="form.produceAddress" placeholder="请输入产地" />
        </el-form-item>
        <el-form-item label="生产地" prop="productionPlace">
          <el-input v-model="form.productionPlace" placeholder="请输入生产地" />
        </el-form-item>
        <el-form-item label="检验机构" prop="testBy">
          <el-input v-model="form.testBy" placeholder="请输入检验机构" />
        </el-form-item>
        <el-form-item label="检测报告" prop="testReport">
          <el-input v-model="form.testReport" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="用户签名" prop="userSignature">
          <el-input v-model="form.userSignature" placeholder="请输入用户签名" />
        </el-form-item>
        <el-form-item label="合格证编号" prop="code">
          <el-input v-model="form.code" placeholder="请输入合格证编号" />
        </el-form-item>
        <el-form-item label="二维码地址" prop="qrcode">
          <el-input v-model="form.qrcode" placeholder="请输入二维码地址" />
        </el-form-item>
        <el-form-item label="打印次数" prop="printNumber">
          <el-input v-model="form.printNumber" placeholder="请输入打印次数" />
        </el-form-item>
        <el-form-item label="是否同步到成都市" prop="isSync">
          <el-select
              v-model="form.isSync"
              placeholder="请选择是否同步到成都市"
              clearable
              style="width: 200px"
          >
            <el-option label="无需同步" :value="0" />
            <el-option label="需要同步" :value="1" />
            <el-option label="已同步" :value="2" />
          </el-select>
        </el-form-item>
        <el-form-item label="省平台追溯码" prop="traceCode">
          <el-input v-model="form.traceCode" placeholder="请输入省平台追溯码" />
        </el-form-item>
<!--        <el-form-item label="删除标记(0:删除,1:正常)" prop="delFlag">-->
<!--          <el-input v-model="form.delFlag" placeholder="请输入删除标记(0:删除,1:正常)" />-->
<!--        </el-form-item>-->
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Certificate">
import { listCertificate, getCertificate, delCertificate, addCertificate, updateCertificate } from "@/api/nongjt/certificate"

const { proxy } = getCurrentInstance()

const certificateList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    productName: null,
    productionPlace: null,
    testBy: null,
    code: null,
    qrType: null,
  },
  rules: {
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询合格证管理列表 */
function getList() {
  loading.value = true
  listCertificate(queryParams.value).then(response => {
    certificateList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    userId: null,
    productName: null,
    productWeight: null,
    unit: null,
    produceAddress: null,
    productionPlace: null,
    testType: null,
    testBy: null,
    testReport: null,
    userSignature: null,
    code: null,
    qrcode: null,
    printNumber: null,
    qrType: null,
    isSync: null,
    traceCode: null,
    batchType: null,
    delFlag: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  }
  proxy.resetForm("certificateRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加合格证管理"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getCertificate(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改合格证管理"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["certificateRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateCertificate(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addCertificate(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除合格证管理编号为"' + _ids + '"的数据项？').then(function() {
    return delCertificate(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('nongjt/certificate/export', {
    ...queryParams.value
  }, `certificate_${new Date().getTime()}.xlsx`)
}

getList()
</script>